<?php
namespace App\Core;


class Request
{
    /**
     * @var array 路由过程中匹配到的URI片段信息
     */
    private $_router_matches = [];
    /**
     * @var App
     */
    protected $_app;

    /**
     * @var array  请求的参数
     */
    protected $_parameters = [];

    public function __construct(App $app)
    {
        $this->_app = $app;
    }

    /**
     * Router类会在在url_mapping匹配后设置
     *
     * @param array $matches
     */
    public function setRouterMatches($matches)
    {
        $this->_router_matches = $matches;
    }

    /**
     * 得到url_mapping匹配的结果
     *
     * @return array
     */
    public function getRouterMatches()
    {
        return $this->_router_matches;
    }

    public function isSecure()
    {
        return isset($_SERVER['HTTPS']);
    }

    public function getMethod()
    {
        return $_SERVER['REQUEST_METHOD'];
    }

    public function getRequestURI()
    {
        return $_SERVER['REQUEST_URI'];
    }

    public function getScriptURI()
    {
        return $_SERVER['SCRIPT_URI'];
    }

    public function getRequestURL()
    {
        return 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
    }

    public function isGet()
    {
        return $this->getMethod() == 'GET';
    }

    public function isPost()
    {
        return $this->getMethod() == 'POST';
    }

    public function getUserAgent()
    {
        return $_SERVER['HTTP_USER_AGENT'];
    }

    public function getCookies()
    {
        return $_COOKIE;
    }

    public function getCookie($name)
    {
        if (!isset($_COOKIE[$name])) {
            return null;
        }
        return $_COOKIE[$name];
    }

    /**
     * 获取所有请求的参数,$_GET&$_POST
     * @return mixed
     */
    public function getParameters()
    {
        if (!$this->_parameters) {
            $this->_parameters = array_merge($_GET, $_POST);
        }
        return $this->_parameters;
    }

    /**
     * 获取请求的参数值
     *
     * @param string $name
     * @return mixed
     */
    public function getParameter($name)
    {
        if (!$this->_parameters) {
            $this->_parameters = $_REQUEST;
        }
        if (isset($this->_parameters[$name])) {
            return $this->_parameters[$name];
        }
        return null;
    }

    protected $_attributes = [];

    /**
     * 类似java HttpRequest的attributes
     *
     * @return array
     */
    public function getAttributes()
    {
        return $this->_attributes;
    }

    public function setAttribute($key, $value)
    {
        $this->_attributes[$key] = $value;
    }

    public function getAttribute($key)
    {
        if (isset($this->_attributes[$key])) {
            return $this->_attributes[$key];
        }
        return null;
    }

    public function removeAttribute($key)
    {
        unset($this->_attributes[$key]);
    }


    protected $client_ip;

    public function getClientIP()
    {
        if ($this->client_ip) {
            $ip = null;

            if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
                $ip_array = explode(",", $_SERVER['HTTP_X_FORWARDED_FOR']);

                for ($i = count($ip_array) - 1; $i >= 0; $i--) {
                    $_ip = trim($ip_array[$i]);
                    if ((!preg_match('/^\d+\.\d+\.\d+\.\d+$/', $_ip)) || preg_match('/^(10|192\.168)\./', $_ip)) {
                        continue;
                    }

                    $tmp = explode('.', $_ip);
                    if ($tmp[0] == 172 && $tmp[1] >= 16 && $tmp[1] <= 31) {
                        continue;
                    }

                    $ip = $_ip;
                    break;
                }
            }

            if (!$ip) {
                $ip = $_SERVER['REMOTE_ADDR'];
            }

            $this->client_ip = $ip;
        }

        return $this->client_ip;
    }
}